﻿using PRO.Admin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PRO.Common;
using System.Data;
using PRO.Product;
using PRO.Entity;
using System.Text.RegularExpressions;

namespace web.Manage.Product
{
    public partial class edit : ManageBase
    {
        protected int id = CVGRequest.GetQueryInt("id", 0);
        protected int cid = CVGRequest.GetQueryInt("cid",0);
        protected string Contents = CVGRequest.GetFormString("txtContents");
        protected ProductInfo productinfo = new ProductInfo();
        protected bool error = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = ClassLib.GetOurClassTree();
                drpClass.Items.Clear();
                drpClass.Items.Add(new ListItem("选择产品分类", "0"));
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    this.drpClass.Items.Add(new ListItem(HttpUtility.HtmlDecode(dt.Rows[i]["ClassName"].ToString()), dt.Rows[i]["id"].ToString()));
                }
               
                Bind();
            }
        }

        private void Bind()
        {
            if (id > 0)
            {
                productinfo = ProductLib.GetProductInfo(id);
                if (productinfo != null && productinfo.ID > 0)
                {
                    if (this.drpClass.Items.FindByValue(productinfo.CID.ToString()) != null)
                        this.drpClass.Items.FindByValue(productinfo.CID.ToString()).Selected = true;
                    this.txtTitle.Text = productinfo.Title;
                    this.txtPrice.Text = productinfo.Price.ToString("0.00");
                    this.txtDes.Text = productinfo.Des;
                    Contents = productinfo.Contents;
                    this.chb_show.Checked = productinfo.Show;
                    cid = productinfo.CID;
                }
                else
                    error = true;
            }
            else
            {
                if (this.drpClass.Items.FindByValue(cid.ToString()) != null)
                    this.drpClass.Items.FindByValue(cid.ToString()).Selected = true;
            }

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.drpClass.SelectedValue.ToInt(0) == 0)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('请选择产品分类！');</script>");
                return;
            }
            if (this.txtTitle.Text.IsNullOrWhiteSpace())
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('请输入产品名称！');</script>");
                return;
            }
            if (this.txtPrice.Text.IsNullOrWhiteSpace())
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('请输入产品价格！');</script>");
                return;
            }
            else
            {
                if (!this.txtPrice.Text.IsPrice())
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('产品价格格式不正确！');</script>");
                    return;
                }
            }
            if (id == 0)
            {
                if (id == 0 && !this.FileUpload_Icon.HasFile)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('请选择您要上传的产品头图！');</script>");
                    return;
                }
            }

            if (CVGRequest.GetFormString("txtContents").IsNullOrWhiteSpace())
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('请输入产品详细介绍！');</script>");
                return;
            }
            string icon_url=null;
            if (this.FileUpload_Icon.HasFile)
            {
                string msg = null;
                icon_url = Utils.SaveRequestImageFile("/UploadFiles/Product/", 500, out msg);
                if (icon_url.IsNullOrWhiteSpace())
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('" + msg + "');</script>");
                    return;
                }
            }
            ProductInfo info = new ProductInfo();
            info.CID = this.drpClass.SelectedValue.ToInt(0);
            info.Title = this.txtTitle.Text;
            info.Price = this.txtPrice.Text.ToDecimal(0);
            info.Show = this.chb_show.Checked;
            info.Icon = icon_url;
            info.Des = this.txtDes.Text;
            info.Contents = CVGRequest.GetFormString("txtContents");
            info.EditTime = DateTime.Now;
            if (id == 0)
            {
                if (ProductLib.AddProduct(info) > 0)
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>if(confirm('添加成功！是否继续添加？')){window.location.href='edit.aspx?cid=" + info.CID + "';}else{window.location.href='default.aspx?cid=" + info.CID + "';}</script>");
                else
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('添加失败！');</script>");
            }
            else if (id > 0)
            {
                info.ID = id;
                if (ProductLib.EditProduct(info))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>if(confirm('编辑成功！是否继续编辑？')){window.location.href='edit.aspx?id=" + info.ID + "';}else{window.location.href='default.aspx?cid=" + this.drpClass.SelectedValue + "';}</script>");
                }
                else
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('编辑失败！');</script>");
            }
        }
         

    }
}